var tips = [{
  fail: '登录数据失败，请稍后再试',
  greeting: '妙思组队平台'
},
{
  fail: 'Login failed, please retry later.',
  greeting: 'Welcome'
}],
nav = [1, 1], // 是否可以跳转, 0为可以跳转

login = new Promise((O_O, X_X) => {   // 登录获取 code
  wx.login({  
    success(code) {
      let url = wx.Config.client ? 'wxLogin' : 'qqLogin';
      wx.post(`Weixin.${url}`, code, res => {
        wx.appData.user = res.data;
        O_O('Login completed.');
      });
    }
  });
}),

load = new Promise((O_O, X_X) => {   // 登录获取 practices 列表
  wx.post('Practice.getPracticeList', {  }, res => {
   // console.log("practices列表：",res)
    wx.appData.practices = res.success ? res.data : [];
    O_O('Load completed.');
  })

  //预拉取发现页面的弹窗提示
  wx.post('Message.getMessage',{
    "Content-Type":"application/json"
  },res=>{
    //console.log("预拉取弹窗：",res);
    wx.appData.broadcast=res.success?res.data:[]
  })
}),

init = new Promise((O_O, X_X) => {
  wx.getSystemInfo({
    success(res) {
     // console.log("systemInfo:",res)
      let system = res;
      system.width = res.windowWidth,
        system.height = res.windowHeight;
      wx.system = system;
      O_O('ReadStorage completed.');
    },
    fail(res) {
      X_X(res);
    }
  });
}),

wait = new Promise((O_O, X_X) => {
  setTimeout(() =>  {
    O_O('Specified minimum time.');
  }, 2000)
});

Page({
  
  onLoad(query) {
    wx.lang = wx.getStorageSync('language') || 0;  
    let greeting = wx.getStorageSync('greeting') || tips[wx.lang].greeting;
    this.setData({
      version: wx.Config.v,
      greeting: greeting
    });
    Promise.all([login, init, load, wait]).then(res => {
      this.nav(--nav[0]);
    }).catch(error => {
      wx.toast(tips[wx.lang].fail);
    });
  },

  onShow() {
    nav = [1, 1];
    let Duration = 800,
      Delay = 500;
    setTimeout(() => {
      this.setData({
        showLogo: true
      });
      wx.show(this, ['logo', 'text']);
      setTimeout(() => {
        this.nav(--nav[1]);
      }, Delay);
    }, Duration / 2 );
    wx.theme = wx.getStorageSync('theme') || '#aaaffb';
    this.bg_color = wx.createAnimation({
      duration: Duration,
      timingFunction: 'linear',
    })
    this.bg_color.opacity(0).step();
    this.setData({
      color: wx.theme,
      bg: this.bg_color.export()
    })
  },

  nav() {
    if (nav[0] || nav[1]) return;
    let Duration = 1000;
    this.bg_color = wx.createAnimation({
      duration: Duration,
      timingFunction: 'ease',
    })
    this.bg_color.opacity(1).step();
    this.setData({
      bg: this.bg_color.export()
    });
    setTimeout(() => {
      wx.switchTab({  url: '../../home/index/index' });
    }, Duration);
  },

  data: {
    showLogo: false,
    greeting: '',
  },

})
